package com.wyt.cloud.utils.query.strategy.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyt.cloud.utils.bean.BeanUtil;
import com.wyt.cloud.utils.exception.BusinessException;
import com.wyt.cloud.utils.query.FilterMatchRuleEnum;
import com.wyt.cloud.utils.query.strategy.AbstractMatchStrategy;
import com.wyt.cloud.utils.query.strategy.QueryMatchStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.Collection;


@Slf4j
@Service
public class NotInStrategy extends AbstractMatchStrategy implements QueryMatchStrategy {
    @Override
    public boolean when(FilterMatchRuleEnum ruleEnum) {
        return FilterMatchRuleEnum.NOT_IN.equals(ruleEnum);
    }

    @Override
    public <T> void then(QueryWrapper<T> wrapper, Field sourceField, Object value, Field targetField) {
        if (!(value instanceof Collection)) {
            log.error("当前字段：{}为{}查询，值类型必须是集合", sourceField.getName(), FilterMatchRuleEnum.NOT_IN.name());
            throw new BusinessException("当前字段：" + sourceField.getName() + "为" + FilterMatchRuleEnum.NOT_IN.name() + "查询，值类型必须是集合");
        }
        wrapper.notIn(BeanUtil.camelToDownLine(getFieldName(sourceField)), ((Collection<?>) value));
    }
}
